<?php
namespace app\index\controller;

use think\Request;
use think\Db;

use app\index\model\praise as PraiseModel;
use app\index\model\answer as AnswerModel;
use app\index\model\user as UserModel;
use app\index\model\expert as ExpertModel;
use app\index\model\message as MessageModel;

class Answer
{
    public function addLookPeopleNum(Request $request)
    {
        //获取请求参数
        $allAnswerToken = $request->param("allAnswerToken");

        //json字符串转成数组
        $allAnswerToken = json_decode($allAnswerToken, true);
        
        //定义返回消息
        $message = "";
        //sql语句
        $answerTab = "dcw_fastask_answer";
        foreach ($allAnswerToken as $key => $value) {
            $sql = "UPDATE $answerTab 
                    SET lookPeopleNum=lookPeopleNum+1 
                    WHERE id=$value";
            $message .= Db::execute($sql) ? 'add lookNum success.' : 'add lookNum fail.';
        }
        $JsonData = controller("index/JsonData");
        $json = $JsonData->getJsonData(0, $message, array());
        return $json;
    }
    public function addNewAnswer(Request $request)
    {
        //保存
        $message = "";

        //获取请求参数
        $questionToken = $request->param("questionToken");
        $answerType = $request->param("answerType");
        $answerContent = $request->param("answerContent");
        $answererToken = $request->param("userToken");
        $recordLongTime = $request->param("recordLongTime");
        $userAvatar = $request->param("userAvatar");
        $userNickname = $request->param("userNickname");
        

        //新建用户模型
        $user = new UserModel;
        $user->userToken = $answererToken;
        $user->userAvatar = $userAvatar;
        $user->userName = $userNickname;
        $userController = controller("index/User");
        $message .= $userController->saveUser($user);
        


        //新建答案模型
        $answer = new AnswerModel;
        $answer->answerType = $answerType;
        $answer->answererToken = $answererToken;
        $answer->questionToken = $questionToken;
        $answer->answerContent = $answerContent;
        $answer->answerTime = date("Y-m-d H:i:s");
        $answer->recordLongTime = $recordLongTime;

        

        if ($answer->save()) {
            $message .= "add answer success.";
        }
        else {
            $message .= "add answer fail.";
        }

        //新增消息模型
        $messageController = controller("index/message");
        $message .= $messageController->addNewMessage($questionToken, $answer["id"]);
        

        //返回消息
        $JsonData = controller("index/JsonData");
        $json = $JsonData->getJsonData(0, $message, array());
        return $json;
        
    }
    public function getQuestionAnswer(Request $request)
    {
        //获取请求参数
        $questionToken = $request->param("questionToken");
        $userToken = $request->param("userToken");

        //查询数据
        $answerTab = "dcw_fastask_answer";
        $questionTab = "dcw_fastask_question";
        $praiseTab = "dcw_fastask_praise";
        $userTab = "dcw_fastask_user";
        $sql = "SELECT 
                $answerTab.*
                FROM $answerTab
                WHERE $answerTab.questionToken=$questionToken
                 ";
        $answer = Db::query($sql);
        
        /** 查询相关数据
         * 查询每个答案是否已点赞
         * 查询回答者的信息
         */
        foreach ($answer as $key => $value) {
            //查询每个答案是否已点赞
            $answerToken = $value["id"];
            $praise = PraiseModel::get(["answerToken" => $answerToken, "userToken" => $userToken]);
            $answer[$key]["isPraise"] = $praise == NULL ? false : true; 
            //查询回答者的信息
            $answererToken = $value["answererToken"];
            $user = UserModel::get(["userToken" => $answererToken]);
            $expert = ExpertModel::get(["expertToken" => $answererToken]);
            $answer[$key]["answererInfo"] = array(
                "avatarUrl" => $user["userAvatar"],
                "name" => $user["userName"],
                "isExpert" => $user["isExpert"],
                "expertIntroduce" => $expert == NULL ? "" : $expert["expertIntroduce"]
            );
        }

        //返回数据
        $JsonData = controller("index/JsonData");
        $json = $JsonData->getJsonData(0, "get answer success.", $answer);
        return $json;
        
    }

    public function givePraise(Request $request)
    {
        //获取请求参数
        $answerToken = $request->param("answerToken");
        $isPraise = $request->param("isPraise");
        $userToken = $request->param("userToken");


        //初始化消息
        $message = "";
        //查询是否已点赞(查询是否存在该记录)
        $note = PraiseModel::get(["answerToken" => $answerToken, "userToken" => $userToken]);

        if ($isPraise) {
            //点赞
            if ($note == NULL) {
                //添加记录
                $newNote = new PraiseModel;
                $newNote->answerToken = $answerToken;
                $newNote->userToken = $userToken;
                if ($newNote->save()) {
                    $message = "add praise success.";
                }
                else {
                    $message = "add praise fail.";
                }
            }
            else {

            }
        }
        else {
            //取消点赞
            if ($note == NULL) {
                
            }
            else {
                //删除记录
                if ($note->delete()) {
                    $message = "delete praise success.";
                }
                else {
                    $message = "delete praise fail.";
                }
            }
        }
        
        $JsonData = controller("index/JsonData");
        $json = $JsonData->getJsonData(0, $message, array());
        return $json;


    }
}